package chat.model;

import java.io.*;

/**  'ChatSender.java'  created by: Team N3 for Swinburne Uni HET715s2/Dr. Recep Ulusoy, 2/10/2012 v0.0 (final)
*
*   A manager for checking and outputing Client text strings to the Chat Server.
*   
* @author Tung Nguyen, Trung V Hoang, Rob Bernard, Justin Semmel
* @since October 2012
*/
public class ChatSender implements ObserverChatSender{

/*
*   ***  1.  Output data stream handler methods (ChatSender)   ***
*                (set up variables and methods)
*/
	private PrintWriter output;
	
	public ChatSender()
	{
	}
		
	/**
	 * initialize the listening thread
	 * @param outStream
	 */
	public ChatSender(OutputStream outStream)
	{
		// Create Error Handler for PrintWriter
		try
		{
			output = new PrintWriter(outStream, true);
		}
		catch(Exception ex) 
		{		
			// O/P stream Error-Handler w. Diag print to CLI
			output.print("Error when create chat ClientSending output stream: " + ex.toString());
		}
	}
	
	/** Create output stream to talk with the server
	 * @param outStream
	 */
	public void createOutputPipe(OutputStream outStream)
	{		
		try
		{
			output = new PrintWriter(outStream, true);
		}
		catch(Exception ex)
		{
			//there is an error here
			output.print("Error when create chat ClientSending output stream: " + ex.toString());
		}
	}
	
	/**
	*  Output message handler (sendMessage)
	*  (O/P of 'Sent' data string [message] to Server)
	*/
	public void sendMessage(String message)
	{
		try
		{
			output.println(message);
		}
		catch(Exception ex)
		{
			output.println("Error when sending message: " + ex.toString());
		}
	}	
}